###########################################################################################################################
# Attachment Upload by URL for vBulletin 3.0.7
# By: Dark Visor (Anthony Kanevsky, ankan925@optonline.net)
# Released: April 20, 2005
# Renta-a-Coder ID: #1360893
# http://www.rentacoder.com/RentACoder/SoftwareCoders/showBioInfo.asp?lngAuthorId=1360893
# Warning: Do not redistribute or modify this hack without my prior consent!
###########################################################################################################################

### What this hack does: ###
---------------------------------------------------------------------------------------------------------------------------
It enchances attachment upload interface with a new 'attachment by url' field :)
---------------------------------------------------------------------------------------------------------------------------

### Features: ###
---------------------------------------------------------------------------------------------------------------------------
[ X ] Uses CURL library to safely upload a file. After that, it uses standard vBulletin API to parse it.
[ X ] Adds an additional field for an URL-based file upload.
---------------------------------------------------------------------------------------------------------------------------

### Version History: ###
---------------------------------------------------------------------------------------------------------------------------
1.00 (beta)
First Public Release (beta)
---------------------------------------------------------------------------------------------------------------------------

Files:
 to Modify: 2
	./newattachment.php
	./includes/functions_file.php

Templates:
 to Modify: 1

Phrases
 to Add: 2

---------------------------------------------------------------------------------------------------------------------------
Time to Install: 1 minute
Skill Level: Easy

---------------------------------------------------------------------------------------------------------------------------
Notes:	This hack is in beta stage so I do not accept any responsibility for any kind of damage to your board.
---------------------------------------------------------------------------------------------------------------------------

Let's Begin!

---------------------------------------------------------------------------------------------------------------------------
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$																				$$$
$$$								FILE CHANGES										$$$
$$$																				$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
---------------------------------------------------------------------------------------------------------------------------

###########################################################################################################################
# ./newattachment.php
###########################################################################################################################

---------------------------------------------------------------------------------------------------------------------------
FIND
---------------------------------------------------------------------------------------------------------------------------

		foreach ($_FILES AS $upload => $attachment)

---------------------------------------------------------------------------------------------------------------------------
ADD BEFORE
---------------------------------------------------------------------------------------------------------------------------

		if (is_array($_POST['attachmenturl']))
		{
			foreach ($_POST['attachmenturl'] AS $key => $url)
			{
				// attempt to fetch the filename from the url
				preg_match('/\/([A-z0-9]*)([\.]{1})([A-z0-9]{2,6})$/si', $url, $matches);
				$filename['original'] = str_replace("/", "", $matches[0]);

				// did we retrieve a valid filename?
				$validurl = iif(!empty($url) AND !empty($filename['original']), true, false);

				if (function_exists('curl_init') AND $validurl)
				{
					// cancel the file that might have been uploaded
					unset($_FILES["attachment$key"]);

					// attempt retrieveing the url
					$curl_handle=curl_init();
					curl_setopt($curl_handle,CURLOPT_URL,$url);
					curl_setopt($curl_handle,CURLOPT_TIMEOUT,15);
					curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,15);
					curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
					curl_setopt($curl_handle,CURLOPT_FAILONERROR,1);
					$data = curl_exec($curl_handle);
					curl_close($curl_handle);

					if (empty($data))
					{ // tsk tsk
						$error = construct_phrase($vbphrase['error_cannot_retrieve_url'], $url);
						$errors[] = array(
							'filename' => htmlspecialchars_uni($url),
							'error' => $error
						);
					}
					else
					{ // ok, let's save the file
						$tmp_name = 'vbupload' . substr(TIMENOW, -4);
						$filesize = strlen($data);
	
						// write file to temporary directory...
						if ($vboptions['safeupload'])
						{
							// ... in safe mode
							$filename['temp'] = $vboptions['tmppath'] . "/$tmp_name";
							$filenum = @fopen($filename['temp'], 'wb');
							@fwrite($filenum, $data);
							@fclose($filenum);
						}
						else
						{
							// ... in normal mode
							$filename['temp'] = tempnam(ini_get('upload_tmp_dir'), 'vbupload');
							$fp = @fopen($filename['temp'], 'wb');
							@fwrite($fp, $data);
							@fclose($fp);
						}

						$_FILES["attachment$key"]['name'] = $filename['original'];
						$_FILES["attachment$key"]['type'] = '';
						$_FILES["attachment$key"]['size'] = $filesize;
						$_FILES["attachment$key"]['tmp_name'] = $filename['temp'];
						$_FILES["attachment$key"]['error'] = 0;
						$_FILES["attachment$key"]['url_upload_by_curl'] = 1;
					}
				}
			}
		}

---------------------------------------------------------------------------------------------------------------------------
FIND
---------------------------------------------------------------------------------------------------------------------------

		$attachinput .= "<input type=\"file\" class=\"bginput\" name=\"attachment$boxcount\" />\n";

---------------------------------------------------------------------------------------------------------------------------
ADD AFTER
---------------------------------------------------------------------------------------------------------------------------

		$attachinput .= "&nbsp;".$vbphrase['or_upload_by_url']."&nbsp;&nbsp;\n";
		$attachinput .= "<input type=\"text\" class=\"bginput\" name=\"attachmenturl[$boxcount]\" /><br />\n";

###########################################################################################################################
# ./includes/functions_file.php
###########################################################################################################################

---------------------------------------------------------------------------------------------------------------------------
FIND
---------------------------------------------------------------------------------------------------------------------------

	if (!is_uploaded_file($attachment))

---------------------------------------------------------------------------------------------------------------------------
REPLACE WITH
---------------------------------------------------------------------------------------------------------------------------

	if (!is_uploaded_file($attachment) AND !$attachment['url_upload_by_curl'])

---------------------------------------------------------------------------------------------------------------------------
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$																				$$$
$$$							TEMPLATE MODIFICATIONS										$$$
$$$																				$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
---------------------------------------------------------------------------------------------------------------------------

###########################################################################################################################
# newattachment
###########################################################################################################################

---------------------------------------------------------------------------------------------------------------------------
FIND
---------------------------------------------------------------------------------------------------------------------------

if ((elm.type == 'file'))

---------------------------------------------------------------------------------------------------------------------------
REPLACE WITH
---------------------------------------------------------------------------------------------------------------------------

if ((elm.type == 'file') || (elm.type == 'text'))

---------------------------------------------------------------------------------------------------------------------------
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$$$																				$$$
$$$								NEW PHRASES											$$$
$$$																				$$$
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
---------------------------------------------------------------------------------------------------------------------------

###########################################################################################################################
phrase: or_upload_by_url
group: GLOBAL
###########################################################################################################################

or upload by url:

###########################################################################################################################
phrase: error_cannot_retrieve_url
group: GLOBAL
###########################################################################################################################

The url (<b>{1}</b>) cannot be retrieved.

###########################################################################################################################
# That's it, you are now done!
###########################################################################################################################